
module.exports = function ($scope, $state, $stateParams,$resource,$modal,items,$modalInstance,FileUploader,flag) {
    console.log('公告');
    console.log(items);
    console.log(flag);
    $scope.a = {
      'project_name' : '',
      'project_file' : '',
      'recommend_type': '',
      'project_type' : '', //项目类别(公告类型)
      'city_project' : '' // 是否为本市项目
    };
    $scope.applyarr=[
        {name:'区县项目',value:'0'},
        {name:'市级项目',value:'1'},
    ]
    $scope.rfoarr=[
        {name:'人事编制处',value:'1'},
        {name:'政策法规处',value:'2'},
        {name:'规划发展处',value:'3'},
        {name:'区域合作处',value:'4'},
        {name:'市场开发处',value:'5'},
        {name:'行业管理处',value:'6'},
        {name:'机关党委',value:'7'},
        {name:'质监所',value:'8'},
        {name:'发展中心',value:'9'},
        {name:'培训中心',value:'10'},
        {name:'协会秘书处',value:'11'},
    ]
    if(flag == 'create') {
        $scope.a = {
            'project_name' : '',
            'project_file' : '',
            'recommend_type': items.recommend_type,
            'project_type' : items.guide_type, //项目类别(公告类型)
            'city_project' : items.city_project // 是否为本市项目
        };
        $scope.item = items;
        for(var i = 0; i < $scope.applyarr.length; i++){ // 是否是市级项目
            if(items.city_project == $scope.applyarr[i].value){
              $scope.item.city_project_name = $scope.applyarr[i].name;
            }
        } 
        for(var i = 0; i < $scope.rfoarr.length; i++){ // 责任处室
            if(items.recommend_type == $scope.rfoarr[i].value){
              $scope.item.recommend_type_name = $scope.rfoarr[i].name;
            }
        } 

    }
    
    if(flag == 'edit'){
        // 详情
        $resource('/api/as/goc/reviewCompanyProject/findInfo', {}, {}).save({'project_code':items.project_code}, function(res){
            if(res.errcode === 0){           
                console.log('详情');
                console.log(res); 
                $scope.item = res.data; 
                $scope.a.project_name = res.data.project_name;
                console.log('名字' + $scope.a.project_name);
                $scope.item.guide_type = res.data.project_type;
                console.log('项目类别' + $scope.item.guide_type);
                $scope.array = res.data.project_file.split(','); // 图片 
                $scope.a.project_file =  res.data.project_file;
                console.log('图片' + $scope.a.project_file);
                for(var i = 0; i < $scope.applyarr.length; i++){ // 是否是市级项目
                    if(res.data.city_project == $scope.applyarr[i].value){
                      $scope.item.city_project_name = $scope.applyarr[i].name;
                    }
                } 
                for(var i = 0; i < $scope.rfoarr.length; i++){ // 责任处室
                    if(res.data.recommend_type == $scope.rfoarr[i].value){
                      $scope.item.recommend_type_name = $scope.rfoarr[i].name;
                    }
                } 
                $scope.a = {
                    'project_code' : res.data.project_code,
                    'project_name' : res.data.project_name,
                    'project_file' : res.data.project_file,
                    'recommend_type': res.data.recommend_type,
                    'project_type' : res.data.project_type, //项目类别(公告类型)
                    'city_project' : res.data.city_project // 是否为本市项目
                };
            }else{
                alert(res.errmsg);
            }
        });
    }
    //调公告类型接口
    $resource('/api/as/goc/reviewGuide/findInfoTypeList', {}, {}).save({}, function(res){
        if(res.errcode === 0){                      
            console.log(res);
            console.log('上面是res');
            $scope.ntarr=[];
            for(var i =0;i<res.data.length;i++){
                $scope.ntarr.push({name:i,value:res.data[i].guide_type});
            }
            console.log($scope.ntarr);
        }else{
            alert(res.errmsg);
        }
    });
    $scope.del=function(index){
        $scope.array.splice(index,1);
        $scope.a.project_file=$scope.array.join(',');
    }
      $scope.image = function(){
      var para = $state.get('app.imageupload');
      //设置上传文件夹，以自己业务命名
      angular.extend(para, {
        resolve : {  
          'dir' : function(){
            return 't1';
          }
        } 
      })
      console.log(para);
      var modalInstance = $modal.open(para);
      modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
        console.log('modal is opened');  
      });  
      modalInstance.result.then(function(result) {  
        console.log(result);  
        $scope.array = result;
        console.log($scope.array.join(','));
        $scope.a.project_file = $scope.array.join(',');
      }, function(reason) {  
        console.log(reason);// 点击空白区域，总会输出backdrop  
        // click，点击取消，则会暑促cancel  
        $log.info('Modal dismissed at: ' + new Date());  
      }); 
    };
    // 提交
    $scope.submit = function(){
        console.log($scope.a.recommend_type);
        console.log($scope.a);
        if($scope.a.project_name==null||$scope.a.project_name==''||$scope.a.project_name==undefined){
            alert('请填写项目名称');
            return;
        }
        if($scope.a.project_file==null||$scope.a.project_file==''||$scope.a.project_file==undefined){
            alert('请上报材料');
            return;
        }
        // console.log($scope.a.project_file);
        // if($scope.a.project_file.length > 1){
        //     $scope.img = $scope.a.project_file.join(",");
        // } else {
        //     $scope.img = $scope.a.project_file[0];
        // }
        // console.log($scope.img);
        // var para = {
        //     recommend_district:$scope.a.city_project,
        //     city_project:$scope.a.city_project,
        //     project_type:$scope.a.project_type,
        //     project_name:$scope.a.project_name,
        //     project_file:$scope.img,
        //     recommend_type:$scope.a.recommend_type
        // }

        console.log($scope.a);
        $resource('/api/ac/goc/reviewCompanyProjectService/createCompanyProject', {}, {}).save($scope.a, function(res){
            if(res.errcode === 0){                      
                alert('提交成功');  
                $modalInstance.close();
            }else{
                alert(res.errmsg);
            }
        });
    }
			
};
